angular.module('hyt.personService', ['ngResource'])

  .factory('addressList',function($resource, $rootScope,ENV,$ionicLoading,$state){

    var ApiUrl = ENV.postUrl+ENV.api.getAddressInfo,
      result;

    var resource = $resource(ApiUrl+':addressList',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getAddressList : function(addressList){
        return resource.query({
          addressList:addressList
        },function(r){
          console.log(r);
          if(r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('AddressList.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('AddAddress', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.addAddress,
      result;
    var resource = $resource(ApiUrl+':addAddress',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getAddAddress : function(addAddress){
        return resource.query({
          addAddress:addAddress
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('AddAddress.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('AddressDetail', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.getAddressDetail,
      result;
    var resource = $resource(ApiUrl+':addressDetail',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getAddressDetail : function(addressDetail){
        return resource.query({
          addressDetail:addressDetail
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('AddressDetail.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('DeleteAddress', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.deleteAddress,
      result;
    var resource = $resource(ApiUrl+':deleteAddress',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getDeleteAddress : function(deleteAddress){
        return resource.query({
          deleteAddress:deleteAddress
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('DeleteAddress.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('Version',function($resource, $rootScope,ENV,$ionicLoading,$state){

    var ApiUrl = ENV.postUrl+ENV.api.getVersion,
      result;

    var resource = $resource(ApiUrl+':version',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getVersion : function(version){
        return resource.query({
          version:version
        },function(r){
          console.log(r);
          if(r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('Version.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('LoadUserDepts',function($resource, $rootScope,ENV,$ionicLoading,$state){

    var ApiUrl = ENV.postUrl+ENV.api.loadUserDepts,
      result;

    var resource = $resource(ApiUrl+':loadUserDepts',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getLoadUserDepts : function(loadUserDepts){
        return resource.query({
          loadUserDepts:loadUserDepts
        },function(r){
          console.log(r);
          if(r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('LoadUserDepts.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('ChangeDept',function($resource, $rootScope,ENV,$ionicLoading,$state){

    var ApiUrl = ENV.postUrl+ENV.api.changeDept,
      result;

    var resource = $resource(ApiUrl+':changeDept',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getChangeDept : function(changeDept){
        return resource.query({
          changeDept:changeDept
        },function(r){
          console.log(r);
          if(r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('ChangeDept.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
